let util = require("./util")
let app = getApp();
let getApi = util.wxPromisify(wx.request,"request"),
    getStorageSync = util.wxPromisify(wx.getStorageSync),
    showLoading = util.wxPromisify(wx.showLoading),
    hideLoading = util.wxPromisify(wx.hideLoading),
    redirectTo = util.wxPromisify(wx.redirectTo),
    requestPayment = util.wxPromisify(wx.requestPayment),
    switchTab = util.wxPromisify(wx.switchTab),
    showToast = util.wxPromisify(wx.showToast),
    NetworkType = util.wxPromisify(wx.getNetworkType),
    showNavigationBarLoading=util.wxPromisify(wx.showNavigationBarLoading),
    hideNavigationBarLoading=util.wxPromisify(wx.hideNavigationBarLoading),
    chooseImage=util.wxPromisify(wx.chooseImage),
    uploadFile=util.wxPromisify(wx.uploadFile,"upfile"),
    showModal=util.wxPromisify(wx.showModal),
    getSystemInfo = util.wxPromisify(wx.getSystemInfo);
//带session的header
const HEADERSESSION = app.globalData.header;
//请求的地址
const BASEURL = app.globalData.baseUrl;
/**
 * 查看购物车商品信息
 */
const getCart = () => {
        return getApi({
            url: BASEURL + '/applet/cart/cartOrderSettlement.do',
            header: HEADERSESSION,
            data: {
                shopCode: app.globalData.common.shopCode
            }
        })
    }
    /**
     * 创建订单
     * @param {*} shopCode 店铺编码
     */
const createOrder = () => {
        return getApi({
            url: BASEURL + '/applet/order/createGoodsOrder.do',
            header: HEADERSESSION,
            data: {
                shopCode: app.globalData.common.shopCode,
                platform: 6
            }
        })
    }
    /**
     * 通过待付款订单号查询支付信息
     * @param {*} code 待付款订单号
     */
const getOrderByCode = (code) => {
        return getApi({
            url: BASEURL + '/applet/order/getOrderByOrderCode.do',
            header: HEADERSESSION,
            data: {
                orderCode: code
            }
        })
    }
    /**
     * 获取用户余额
     */
const getBalance = () => {
       
        return getApi({
            url: BASEURL + '/wxappletMember/getBalance.do',
            header: HEADERSESSION
        })
    }
    /**
     * 获取用户级别
     */
const getLevel = () => {
        return getApi({
            url: BASEURL + '/member/getMember.do',
            header: HEADERSESSION
        })
    }
    /**
     * 查询订单
     * @param {*} status 订单状态： 100/null =>全部订单； 0=》待支付订单 ；2=>已完成
     * @param {*} pageNum 当前页码 默认1
     * @param {*} pageSize 每页显示数量 默认10
     */
const getOrders = (status, pageNum = 1, pageSize = 10) => {
      
        return getApi({
            url: BASEURL + '/applet/order/getOrderList.do',
            header: HEADERSESSION,
            data: {
                orderStatus: status,
                pageNum: pageNum,
                pageSize: pageSize
            }
        })
    }
    /**
     * 获取支付需要的参数，如果是微信支付，会把获取的参数作为调用微信支付的参数
     * @param {*} ordeCode 订单号
     * @param {*} payType 支付方式 1=》微信支付  4=》余额支付
     */
const getPayParams = (ordeCode, payType) => {
        return getApi({
            url: BASEURL + '/pay/doPay.do',
            header: HEADERSESSION,
            data: {
                orderCode: ordeCode,
                payType: payType,
                platform: 6
            }
        })
    }
    /**
     * 获取验证码
     * @param {*手机号} phonenum 
     */
const acquireCode = (phonenum) => {
        return getApi({
            url: BASEURL + '/verify/code.do',
            data: {
                mobile: phonenum
            }
        })
    }
    /**
     * 登录
     * @param {*手机号} phonenum 
     * @param {*验证码} identifyCode 
     */
const login = (phonenum, identifyCode) => {
        return getApi({
            url: BASEURL + '/wxappletMember/bind.do',
            data: {
                mobile: phonenum,
                vc: identifyCode
            }
        })
    }
    /**
     * 绑定手机号
     * @param {*手机号} mobile 
     * @param {*验证码} vc 
     */
const bindPhone = (openId,platform,shopCode,userInfo,mobile, vc) => {
        return getApi({
            url: BASEURL + '/applet/register.do',
            // header: {
            //     Cookie: HEADERSESSION.Cookie,
            //     'content-type': 'application/json' 
            // },
            header: HEADERSESSION,
            method: "POST",
            data: {
                openId:openId,
                platform:platform,
                shopCode:shopCode,
                userInfo:userInfo,
                mobile: mobile,
                vc: vc,
            }
        })
    }
/**
 * 检测人脸质量接口
 * @param {*文件路径} filePath 
 */
const faceCheck=(filePath)=>{
    return uploadFile({
        url: BASEURL + '/shop/validateImgQuality.do',
        filePath:filePath,
        name:"file",
            header: {
            Cookie: HEADERSESSION.Cookie,
            'content-type': 'multipart/form-data' 
        },
    })
}
/**
 * 获取上传头像或人脸到阿里云oss的签名参数
 * @param {*} businessType 1=>人脸的标 识2=>头像标识
 */
const getImageSign=(businessType)=>{
    return getApi({
        url: BASEURL+'/image/getImageSign.do',
        method: "POST",
        header: {
            Cookie: HEADERSESSION.Cookie,
            'content-type': 'application/x-www-form-urlencoded' 
        },
        data:{
            businessType:businessType
        }
    })
}
const uptoAliOSS=(obj,filePath)=>{
    return uploadFile({
        url: "http://testupload.24xiaomai.com",
        filePath:filePath,
        name:"file",
        formData:{
            OSSAccessKeyId:obj.accessid,
            policy:obj.policy,
            Signature:obj.signature,
            key:obj.filePath + "/"+obj.fileName,
            success_action_status:"200",
            callback:obj.callback
        }
    })
}
/**
 * 检测用户是否上传了人脸识别照片
 */
const hasUploadImg=()=>{
    return getApi({
        url: BASEURL + '/member/getFaceImg.do',
        header: HEADERSESSION
    })
}
    /**
     * 检查API的在当前的微信版本是否可用
     * @param {*需要检测的API} API 
     */
const compatibilityHandler = (API) => {
    if (API) {
        API()
    } else {
        // 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
        wx.showModal({
            title: '提示',
            content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
        })
    }
}

const handleFetchError = (des) => {
        wx.showToast({
            title: des,
            image: '../../image/warn.png',
            duration: 3000
        })
    }
    /**
     * 
     * @param {*当前的调用者} self 
     * @param {* 提示的文案} my_toast_tip 
     * @param {*选择的模板 } template 
     * @param {*模板自动消失的时间} time 
     */
const showMyToast = (self, my_toast_tip, time = 2000, template = 'my_toast') => {
    self.setData({
        template: template, //my_toast
        my_toast_tip: my_toast_tip
    })
    setTimeout(() => {
        self.setData({
            template: '', //my_toast
            my_toast_tip: ''
        })
    }, time)
}
const loadingText=(config={ title: '加载中' })=>{
    showLoading(config);
    showNavigationBarLoading()

}
const hideLoadingText=()=>{
    hideLoading();
    hideNavigationBarLoading()
}

module.exports = {
    showLoading,
    hideLoading,
    getCart,
    getBalance,
    createOrder,
    getOrders,
    getOrderByCode,
    requestPayment,
    getPayParams,
    redirectTo,
    switchTab,
    acquireCode,
    login,
    getSystemInfo,
    showToast,
    bindPhone,
    compatibilityHandler,
    handleFetchError,
    getLevel,
    showMyToast,
    NetworkType,
    loadingText,
    hideLoadingText,
    chooseImage,
    faceCheck,
    showModal,
    getImageSign,
    uptoAliOSS,
    hasUploadImg
}